package com.website.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.website.common.controller.BaseController;
import com.website.common.warpper.ResultMap;
import com.website.system.model.ComPlainType;
import com.website.system.model.Complain;
import com.website.system.model.User;
import com.website.system.model.vo.ComplainVO;
import com.website.system.service.ComPlainTypeService;
import com.website.system.service.ComplainService;
import com.website.system.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("manage/complaints")
public class ComplainController extends BaseController {

    @Resource
    private ComplainService complainService;

    @Resource
    private ComPlainTypeService comPlainTypeService;

    @Resource
    private UserService userService;

    /**
     * 跳转到 投诉管理
     */
    @RequestMapping("")
    public String article() {
        return "manage/complaints";
    }

    /**
     * 加载数据 投诉信息
     */
    @RequestMapping("/complainTable")
    @ResponseBody
    public ResultMap complainTable(@RequestParam Integer page, @RequestParam Integer limit,@RequestParam(required =false) String complaintsContent) {
        ResultMap<List<ComplainVO>> resultMap = new ResultMap<>(SUCCESS_CODE, SUCCESS_MSG, null, null);
        Wrapper<Complain> wrapper = new EntityWrapper<>();
        wrapper = complaintsContent == null ? wrapper : wrapper.like("complaints_content", complaintsContent);
        Page<Complain> pageInfo = new Page<>(page,limit);
        List<Complain> complainList = complainService.selectPage(pageInfo,wrapper.orderBy("create_time", false)).getRecords();
        resultMap.setCount(complainService.selectCount(wrapper));
        List<ComplainVO> complainVOList  = new ArrayList<>();
        for (Complain complain: complainList) {
            ComplainVO complainVO = new ComplainVO();
            ComPlainType comPlainType = comPlainTypeService.selectById(complain.getComplaintsType());
            User user = userService.selectById(complain.getUserId());
            complainVO.setId(complain.getId());
            complainVO.setUserName(user.getName());
            complainVO.setComplaintsType(complain.getComplaintsType());
            complainVO.setComplaintsContent(complain.getComplaintsContent());
            complainVO.setCreateTime(complain.getCreateTime());
            complainVO.setName(comPlainType.getName());
            complainVO.setImg(complain.getImg());
            complainVOList.add(complainVO);
        }

        resultMap.setData(complainVOList);
        return  resultMap;
    }

    /**
     * 查看页面 投诉信息
     */
    @RequestMapping("/toComplainRead/{type}")
    public String complainRead(Model model, @PathVariable Integer type) {
        Complain complain = complainService.selectById(type);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = formatter.format(complain.getCreateTime());
        model.addAttribute("complaints", complain);
        model.addAttribute("createTime", createTime);
        ComPlainType comPlainType = comPlainTypeService.selectById(complain.getComplaintsType());
        //存放类别对应的类别信息
        model.addAttribute("comPlainType", comPlainType);
        return "manage/complainRead";
    }

    /**
     * 编辑页面 投诉信息
     */
    @RequestMapping("/toComplainUpdate/{type}")
    public String toComplainUpdate(Model model, @PathVariable Integer type) {
        Complain complain = complainService.selectById(type);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = formatter.format(complain.getCreateTime());
        model.addAttribute("complaint", complain);
        model.addAttribute("createTimes", createTime);
        ComPlainType comPlainType = comPlainTypeService.selectById(complain.getComplaintsType());
        //存放类别对应的类别信息
        model.addAttribute("comPlainType", comPlainType);
        return "manage/complainUpdate";
    }

    /**
     * 保存修改 投诉信息
     */
    @ResponseBody
    @RequestMapping("/complainUpdate")
    public String complainUpdate(Complain complain) {
        try{
            complainService.updateById(complain);
            return SUCCESS_MSG;
        }catch (Exception e){
            return ERROR_MSG;
        }
    }

    /**
     * 查询所有投诉类别
     */
    @RequestMapping("/complainTypeAll")
    @ResponseBody
    public List<ComPlainType> complainTypeAll(){
        return comPlainTypeService.selectList(new EntityWrapper<>());
    }

    /**
     * 添加页面 投诉信息
     */
    @RequestMapping("/toComplainAdd")
    public String complainAdd(){
        return  "manage/complainAdd";
    }

    /**
     * 保存添加 投诉信息
     */
    @ResponseBody
    @RequestMapping("/complainAdd")
    public String complainAdd(@RequestParam Integer complaintsType,@RequestParam String complaintsContent) {
            User user = getCurrentUser();
            int userId =user.getId();
            try{
                Complain complain = new Complain();
                complain.setUserId(userId);
                complain.setComplaintsType(complaintsType);
                complain.setComplaintsContent(complaintsContent);
                complain.setCreateTime(new Date());
                complainService.insert(complain);
                return SUCCESS_MSG;
            }catch (Exception e){
                return ERROR_MSG;
            }
    }

    /**
     * 删除数据 投诉信息
     */
    @ResponseBody
    @RequestMapping("/toComplainDel")
    public String complainDel(@RequestParam Integer id) {
        try{
            complainService.deleteById(id);
            return SUCCESS_MSG;
        }catch (Exception e){
            return ERROR_MSG;
        }
    }

    /**
     * 图片页面 投诉信息
     */
    @RequestMapping("/uploadImg/{type}")
    public String uploadImg(Model model, @PathVariable Integer type) {
        Complain complain = complainService.selectById(type);
        model.addAttribute("complaint", complain);
        return "manage/uploadImg";
    }
}
